package org.gavin.core.utils;

import java.util.regex.Pattern;

/**
 * @author grl
 * @date 2022/4/11
 * Created by admin on 2022/4/11 9:01
 */
public class ValidatorUtils {
    /**
     * 电话号码验证正则
     * "^[1]\\d{10}$"
     */
    public static final Pattern phonePattern = Pattern.compile("^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$");
    /**
     * 通知Code验证正则
     */
    private static final Pattern codePattern = Pattern.compile("^\\d{6}$");
    /**
     * ip正则
     */
    private static final Pattern IP = Pattern.compile("^((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])$");

    /**
     * 内容不能包含签名专用符 【】 []
     * 内容不能包含特殊符号
     *
     * @param name
     * @return
     */
    public static boolean containSpecial(String name) {
        if (isContains(name)) {
            return true;
        }
        if (hasSpecial(name)) {
            return true;
        }
        return false;
    }

    /**
     * 特殊符号
     *
     * @param name
     * @return boolean true 包含 false 不包含
     */
    public static boolean hasSpecial(String name) {
        if (name.contains("&")
                || name.contains("@")
                || name.contains("^")
                || name.contains("<")
                || name.contains(">")
                || name.contains("#")
                || name.contains("_")
        ) {
            return true;
        }
        return false;
    }

    /**
     * 判断Ip是否合法
     *
     * @param ipStr
     * @return true 合法
     */
    public static boolean isIp(String ipStr){
        return ValidatorUtils.IP.matcher(ipStr).matches();
    }


    private static boolean isContains(String name) {
        return (name.contains("[") && name.contains("]")) || (name.contains("【") && name.contains("】"));
    }

}
